package com.kuzufang.modules.main.room;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kuzufang.R;
import com.kuzufang.base.BaseAdapter;
import com.kuzufang.bean.RoomBean;
import com.kuzufang.modules.imageutil.ImageUrls;
import com.kuzufang.utils.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by songyuanjin on 2017/9/18.
 */

public class RoomAdapter extends BaseAdapter<RoomBean, RoomAdapter.RoomHolder> {
    private Activity activity;

    public RoomAdapter(Context ctx) {
        super(ctx);
        activity = (Activity) ctx;

    }


    public void setData() {
        clear();
        List<RoomBean> roomBeen = new ArrayList<>();
        List<String> images = ImageUrls.imageUrls();
        for (int i = 0; i < images.size(); i++) {
            RoomBean roomBean = new RoomBean();
            roomBean.setUrl(images.get(i));
            roomBeen.add(roomBean);
        }
        addAll(roomBeen);
        notifyDataSetChanged();
    }

    @Override
    protected void bindViewHolderData(RoomHolder viewHolder, RoomBean data, int position) {
        viewHolder.bindData(data, position);
    }

    @Override
    public RoomHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mCtx).inflate(R.layout.item_room_list, parent, false);
        return new RoomHolder(view);
    }

    class RoomHolder extends RecyclerView.ViewHolder {
        private CardView itemCard;

        public RoomHolder(View itemView) {
            super(itemView);
            itemCard = (CardView) itemView.findViewById(R.id.item_card);
        }

        public void bindData(final RoomBean roomBean, int position) {
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemCard.getLayoutParams();
            if (position == 0) {
                layoutParams.topMargin = 0;
            } else {
                layoutParams.topMargin = DisplayUtils.dp2px(12);
            }
            itemCard.setLayoutParams(layoutParams);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, FindRoomDetailActivity.class);
                    intent.putExtra("room_detail", roomBean.getUrl());
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                        ActivityOptionsCompat options =
                                ActivityOptionsCompat.makeScaleUpAnimation(itemView,
                                        itemView.getWidth() / 2, itemView.getHeight() / 2, 0, 0);
                        ActivityCompat.startActivity(itemView.getContext(), intent, options.toBundle());
                    } else {
                        activity.startActivity(intent);
                    }
                }
            });
        }
    }

}
